package cn.servlet;

import util.JdbcHelper;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.*;

@WebServlet("/signin")
public class UserSignIn extends HttpServlet {
    private String driver="com.mysql.cj.jdbc.Driver";
    private String url="jdbc:mysql://localhost:3306/ecut?serverTimezone=Asia/Shanghai&useSSL=false&useServerPrepStmts=true&cachePrepStmts=true";
    private String user="manager";
    private String password="Admin2020";
    @Override
    public void service(ServletRequest request, ServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String name=request.getParameter("name");
        String gender=request.getParameter("gender");
        if(gender.equals("M")){
            gender="男";
        }
        else if(gender.equals("F")){
            gender="女";
        }
        PrintWriter pw=response.getWriter();
        int id=Integer.parseInt(request.getParameter("id"));
        Date date=Date.valueOf(request.getParameter("birthdate"));
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection conn= null;
        try {
            conn = DriverManager.getConnection(url,user,password);

        String sql="insert into t_users values (?,?,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1,id);
        ps.setString(2,name);
        ps.setString(3,gender);
        ps.setDate(4,date);
        int count=ps.executeUpdate();
        System.out.println(count);
        ps.close();
        conn.close();
        pw.print("<h2 style='text-align:center'>");
        pw.println("注册成功，欢迎"+name +"</h1>");
        pw.print("<div style='margin:auto;text-align:center;color:red;text-decoration：none;font-size:16px'><a href='index.html'>");
        pw.println("返回主页</a> |");
        pw.print(" <a style='color:red,text-decoration：none;font-size:16px' href='/userlist'>");
        pw.println("查看所有用户信息</a></div>");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

}
